﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ShapeDemo.UC.WinProp;
using System.Windows;

namespace ShapeDemo
{
    /// <summary>
    /// 画Polygon的相关方法
    /// </summary>
    public class PolygonDrawer
    {
        private Canvas polygonCanvas;
        private List<ASPNETX_WPFPolygon> lstPolygon;
        private WinPropPolygon winPolygonProp;
        private WinPropCommon winCommonProp;
        
        ASPNETX_WPFPolygon pgBody;
        TMouse MouseType;

        private ContextMenu cMenu = new ContextMenu();

        public PolygonDrawer(Canvas canvasContent, List<ASPNETX_WPFPolygon> lstShape, WinPropPolygon winShapeProp,WinPropCommon winProp)
        {
            polygonCanvas = canvasContent;
            lstPolygon = lstShape;
            winPolygonProp = winShapeProp;
            winCommonProp = winProp;

            MouseType = TMouse.Drawing;

            MenuItem miFinish = new MenuItem();
            miFinish.Header = "Finish";
            miFinish.Click += new RoutedEventHandler(MenuItem_Finish_Click);

            MenuItem miDel = new MenuItem();
            miDel.Header = "Delete previous point";
            miDel.Click += new RoutedEventHandler(MenuItem_DelLastPoint_Click);

            MenuItem miClear = new MenuItem();
            miClear.Header = "Clear";
            miClear.Click += new RoutedEventHandler(MenuItem_Redraw_Click);

            cMenu.Items.Add(miFinish);
            cMenu.Items.Add(miDel);
            cMenu.Items.Add(miClear);

            polygonCanvas.ContextMenu = cMenu;

            polygonCanvas.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(polygonCanvas_MouseLeftButtonDown);
            polygonCanvas.MouseMove += new System.Windows.Input.MouseEventHandler(polygonCanvas_MouseMove);
        }

        public void Draw()
        {
            pgBody = ResourceCenter.NewPolygon();
            polygonCanvas.Children.Add(pgBody.PolygonBody);
        }

        #region 处理鼠标事件
        void polygonCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (MouseType == TMouse.Drawing)
            {
                if (pgBody.PolygonBody.Points.Count > 0)
                {
                    Point p = e.GetPosition(polygonCanvas);
                    int pgPointCount = pgBody.PolygonBody.Points.Count;

                    pgBody.PolygonBody.Points[pgPointCount - 1] = p;

                    winPolygonProp.X = p.X;
                    winPolygonProp.Y = p.Y;
                }
            }
        }

        void polygonCanvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (MouseType == TMouse.Drawing)
            {
                Point p = e.GetPosition(polygonCanvas);

                //预览点
                //Rectangle rect = new Rectangle();
                //rect.Fill = new SolidColorBrush(Colors.Green);
                //rect.SetValue(Canvas.LeftProperty, p.X - 2.5);
                //rect.SetValue(Canvas.TopProperty, p.Y - 2.5);
                //rect.Width = 5;
                //rect.Height = 5;

                //polygonCanvas.Children.Add(rect);

                int pgPointCount = pgBody.PolygonBody.Points.Count;
                pgBody.PolygonBody.Points[pgPointCount - 1] = p;
                pgBody.PolygonBody.Points.Add(p);

                winPolygonProp.lstPointsList.Items.Add("(" + p.X + "," + p.Y + ")");
            }
        }
        #endregion

        #region 上下文菜单的处理
        /// <summary>
        /// 上下文菜单的完成按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Finish_Click(object sender, RoutedEventArgs e)
        {
            if (pgBody.PolygonBody.Points.Count > 2)
            {
                //1.移除最后一个点，因为其是昨预览线用的
                //2.设置基本属性值
                //3.将其添加到面序列，至此画面工作完成，为画下一组面做准备
                //4.新实例化一个面，并将其添加到画板中
                //5.绑定层次列表

                pgBody.PolygonBody.Points.RemoveAt(pgBody.PolygonBody.Points.Count - 1);

                pgBody.CommonProperty.Index = winCommonProp.tbIndex.Text;
                pgBody.CommonProperty.Content = winCommonProp.tbContent.Text;

                lstPolygon.Add(pgBody);

                pgBody = ResourceCenter.NewPolygon();
                winPolygonProp.lstPointsList.Items.Clear();

                polygonCanvas.Children.Add(pgBody.PolygonBody);

                //BindListBox();
            }
            else
            {
                MessageBox.Show("Polygon has not finished yet!");
            }
        }

        /// <summary>
        /// 上下文菜单的删除上一个点按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_DelLastPoint_Click(object sender, RoutedEventArgs e)
        {
            if (pgBody.PolygonBody.Points.Count > 2)
            {
                pgBody.PolygonBody.Points.RemoveAt(pgBody.PolygonBody.Points.Count - 1);

                //BindListBox();
            }
            else
            {
                MessageBox.Show("One point must be remain!");
            }
        }

        /// <summary>
        /// 上下文菜单重画按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Redraw_Click(object sender, RoutedEventArgs e)
        {
            polygonCanvas.Children.Remove(pgBody.PolygonBody);

            pgBody = ResourceCenter.NewPolygon();

            polygonCanvas.Children.Add(pgBody.PolygonBody);
        }
        #endregion
    }
}
